package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderDictEntity3;
import com.sgb.kjwl.view.ui.activity.pick.PhotoDownloadViewActivity;
import com.sgb.link.view.widget.ExpandedTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.math.BigDecimal;
import java.util.List;

public class PickDetailRequireAdapter extends BaseRecycleAdapter<TenderDictEntity3> {

    private Context context;
    private String title;

    public PickDetailRequireAdapter(Context context, List<TenderDictEntity3> datas) {
        super(context, R.layout.item_pick_detial_require3, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, final TenderDictEntity3 entity) {
        holder.setBinding(BR.viewModel, entity);
        TextView contentView = holder.getItem().findViewById(R.id.content_view);
        TextView expandView = holder.getItem().findViewById(R.id.expand_view);
        TextView contentTitle = holder.getItem().findViewById(R.id.content_title);
        ImageView imageFile = holder.getItem().findViewById(R.id.image_file);
        View viewHint = holder.getItem().findViewById(R.id.hint);
        TextView tvType = holder.getItem().findViewById(R.id.tv_type);

        String nameItem = entity.getName();
        if (!TextUtils.isEmpty(nameItem)) {
            int length = nameItem.length();
            if (length > 5) {
                int mult = length / 4;
                if (mult == 0) {
                    contentTitle.setText(nameItem);
                } else {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mult; i++) {
                        sb.append(nameItem.substring(4 * i, 4 * i + 4));
                        if (i < mult - 1) {
                            sb.append("\n");
                        }
                    }
                    if (mult > 0 && length % 4 > 0) {
                        sb.append("\n");
                        sb.append(nameItem.substring(mult * 4));
                    }
                    contentTitle.setText(sb.toString());
                }
            }
        }

        if (datas.get(datas.size() - 1).equals(entity)) {   //最后一个
            if (!"中标价".equals(entity.getName()) && !"中标单价".equals(entity.getName())) {
                viewHint.setVisibility(View.VISIBLE);
            }
        } else {
            viewHint.setVisibility(View.GONE);
        }

        if ("中标价".equals(entity.getName()) || "中标单价".equals(entity.getName())) {
            holder.getItem().setBackground(context.getResources().getDrawable(R.drawable.detial_item_blue));
            contentTitle.setTextColor(context.getResources().getColor(R.color.white));
            contentView.setTextColor(context.getResources().getColor(R.color.white));
            if (!TextUtils.isEmpty(contentView.getText())) {
                BigDecimal bg = new BigDecimal(contentView.getText().toString());
                String text = bg.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
                contentView.setText(text);
            }
        } else {
            contentTitle.setTextColor(context.getResources().getColor(R.color.color_333333));
            contentView.setTextColor(context.getResources().getColor(R.color.color_333333));
            String value = entity.getValue();
            try {
                if (!TextUtils.isEmpty(value)) {
                    double aDouble = Double.parseDouble(value);
                    BigDecimal bg = new BigDecimal(aDouble);
                    String text = bg.setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString();
                    contentView.setText(text);
                }
            } catch (Exception e) {
            }
        }


        if (!TextUtils.isEmpty(entity.getFileUrl()) && entity.getFileUrl().indexOf("http") != -1) {
            if (entity.getFileUrl().contains(".png") || entity.getFileUrl().contains(".jpg") || entity.getFileUrl().contains(".gif")) {
                tvType.setText("预览");
                updateView(tvType, entity.getFileUrl(), 2);
                updateView(contentView, entity.getFileUrl(), 2);
            } else {
                tvType.setText("下载");
                updateView(tvType, entity.getFileUrl(), 1);
                updateView(contentView, entity.getFileUrl(), 1);
            }
            if (!TextUtils.isEmpty(entity.getValue())) {
                contentView.setText(entity.getValue());
            } else {
                contentView.setText("附件");
            }
            contentView.setMaxLines(1);
            contentView.setEllipsize(TextUtils.TruncateAt.END);
            contentView.setTextColor(context.getResources().getColor(R.color.color_5792FD));
            imageFile.setVisibility(View.VISIBLE);
            tvType.setVisibility(View.VISIBLE);
        } else if (!TextUtils.isEmpty(entity.getValue()) && entity.getValue().indexOf("http") != -1) {
            if (entity.getValue().contains(".png") || entity.getValue().contains(".jpg") || entity.getValue().contains(".gif")) {
                tvType.setText("预览");
                updateView(tvType, entity.getValue(), 2);
                updateView(contentView, entity.getValue(), 2);
            } else {
                tvType.setText("下载");
                updateView(tvType, entity.getValue(), 1);
                updateView(contentView, entity.getValue(), 1);
            }
            contentView.setText("附件");
            contentView.setMaxLines(1);
            contentView.setEllipsize(TextUtils.TruncateAt.END);
            contentView.setTextColor(context.getResources().getColor(R.color.color_5792FD));
            imageFile.setVisibility(View.VISIBLE);
            tvType.setVisibility(View.VISIBLE);
        } else {
            contentView.setMaxLines(3);
            new ExpandedTextView().showTextPick(contentView, expandView);
            imageFile.setVisibility(View.GONE);
            tvType.setVisibility(View.GONE);
        }

    }

    private void updateView(View view, String url, int type) {

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(url)) {
                    if (type == 1) {
                        //调用系统浏览器下载
                        Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        intent.putExtra("title", title);
                        context.startActivity(intent);
                    } else if (type == 2) {
                        Intent intent = new Intent(context, PhotoDownloadViewActivity.class);
                        intent.putExtra("url", url);
                        intent.putExtra("title", title);
                        context.startActivity(intent);
                    }
                }
            }
        });

    }

    public void setTitle(String title){
        this.title = title;
    }
}
